package com.example.smartcitydemo.ui.subway.subway.fragment

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.LineDetail
import com.example.smartcitydemo.databinding.FragmentSubwayDetailBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ui.subway.SubWayDetailAdapter
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWaySharedViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.subway.subway
 * @NAME: Caleb.F
 * @DATE: 2021/11/16 9:48
 * @Email: 1919796283@qq.com
 */
class SubWayDetailFragment :
    BaseFragment<FragmentSubwayDetailBinding>(R.layout.fragment_subway_detail) {
    private val subWaySharedViewModel: SubWaySharedViewModel by activityViewModels()
    private val subDetailAdapter: SubWayDetailAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        SubWayDetailAdapter().apply {
            init()
            setDiffCallback(LineDetail.diffCallBack)
        }
    }

    override fun initData() {

    }

    override fun initView(bindView: FragmentSubwayDetailBinding, savedInstanceState: Bundle?) {
        bindView.toolbarSubwayDetail.tvContent.text = "地铁详情"
        bindView.rvSubwayStationDetail.adapter = subDetailAdapter
    }

    override fun addObservers(bindView: FragmentSubwayDetailBinding) {
        subWaySharedViewModel.subWayDetail.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success -> {
                    bindView.tvStationName.text = it.data?.stepInfo?.name
                    subDetailAdapter.setDiffNewData(it.data?.lineList?.toMutableList())
                }
            }
        }
    }
}